import React from 'react';
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
import { useAuth } from '../hooks/useAuth';

// 页面组件
import Login from '../pages/Login';
import Register from '../pages/Register';
import Dashboard from '../pages/Dashboard';
import Portfolio from '../pages/Portfolio';
import PortfolioDetail from '../pages/PortfolioDetail';
import Products from '../pages/Products';
import ProductDetail from '../pages/ProductDetail';
import FundData from '../pages/FundData';
import FundDetail from '../pages/FundDetail';
import FundDetailChart from '../pages/FundDetailChart';
import FundInfoDetail from '../pages/FundInfoDetail';
import FundNavRanking from '../pages/FundNavRanking';

import FundDataDownload from '../pages/FundDataDownload';
import FundDataDownloader from '../pages/FundDataDownloader';
import Profile from '../pages/Profile';
import Settings from '../pages/Settings';

// 管理后台组件
import AdminLayout from '../components/Admin/AdminLayout';
import AdminDashboard from '../pages/Admin/Dashboard';
import UserManagement from '../pages/Admin/UserManagement';
import ProductManagement from '../pages/Admin/ProductManagement';
import InvestmentManagement from '../pages/Admin/InvestmentManagement';
import DataAnalysis from '../pages/Admin/DataAnalysis';
import SystemManagement from '../pages/Admin/SystemManagement';
import MonitoringCenter from '../pages/Admin/MonitoringCenter';
import SecurityCenter from '../pages/Admin/SecurityCenter';

// 受保护的路由组件
const ProtectedRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const { isAuthenticated } = useAuth();
  const location = useLocation();
  
  if (!isAuthenticated) {
    // 保存用户原本想访问的页面，登录后可以跳转回来
    return <Navigate to="/login" state={{ from: location }} replace />;
  }
  
  return <>{children}</>;
};

// 公开路由组件（已登录用户不能访问）
const PublicRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const { isAuthenticated } = useAuth();
  
  if (isAuthenticated) {
    return <Navigate to="/dashboard" replace />;
  }
  
  return <>{children}</>;
};

// 管理员路由组件（需要管理员权限）
const AdminRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const { isAuthenticated, user } = useAuth();
  
  if (!isAuthenticated) {
    return <Navigate to="/login" replace />;
  }
  
  // 检查用户是否有管理员权限
  if (!user?.isAdmin) {
    return <Navigate to="/dashboard" replace />;
  }
  
  return <>{children}</>;
};

const AppRoutes: React.FC = () => {
  return (
    <Routes>
      {/* 公开路由 */}
      <Route
        path="/login"
        element={
          <PublicRoute>
            <Login />
          </PublicRoute>
        }
      />
      <Route
        path="/register"
        element={
          <PublicRoute>
            <Register />
          </PublicRoute>
        }
      />
      
      {/* 受保护的路由 */}
      <Route
        path="/dashboard"
        element={
          <ProtectedRoute>
            <Dashboard />
          </ProtectedRoute>
        }
      />
      <Route
        path="/portfolio"
        element={
          <ProtectedRoute>
            <Portfolio />
          </ProtectedRoute>
        }
      />
      <Route
        path="/portfolio/:id"
        element={
          <ProtectedRoute>
            <PortfolioDetail />
          </ProtectedRoute>
        }
      />
      <Route
        path="/products"
        element={
          <ProtectedRoute>
            <Products />
          </ProtectedRoute>
        }
      />
      <Route
        path="/products/:id"
        element={
          <ProtectedRoute>
            <ProductDetail />
          </ProtectedRoute>
        }
      />
      <Route
        path="/funds"
        element={
          <ProtectedRoute>
            <FundData />
          </ProtectedRoute>
        }
      />
      <Route
        path="/fund-info-detail"
        element={
          <ProtectedRoute>
            <FundInfoDetail />
          </ProtectedRoute>
        }
      />
      <Route
        path="/funds/:code"
        element={
          <ProtectedRoute>
            <FundDetail />
          </ProtectedRoute>
        }
      />
      <Route
        path="/fund-detail-chart"
        element={
          <ProtectedRoute>
            <FundDetailChart />
          </ProtectedRoute>
        }
      />

      <Route
        path="/fund-nav-ranking"
        element={
          <ProtectedRoute>
            <FundNavRanking />
          </ProtectedRoute>
        }
      />

      <Route
        path="/fund-data-download"
        element={
          <ProtectedRoute>
            <FundDataDownload />
          </ProtectedRoute>
        }
      />
      <Route
        path="/fund-data-downloader"
        element={
          <ProtectedRoute>
            <FundDataDownloader />
          </ProtectedRoute>
        }
      />
      <Route
        path="/profile"
        element={
          <ProtectedRoute>
            <Profile />
          </ProtectedRoute>
        }
      />
      <Route
        path="/settings"
        element={
          <ProtectedRoute>
            <Settings />
          </ProtectedRoute>
        }
      />
      
      {/* 管理后台路由 */}
      <Route
        path="/admin/*"
        element={
          <AdminRoute>
            <AdminLayout />
          </AdminRoute>
        }
      >
        <Route index element={<AdminDashboard />} />
        <Route path="dashboard" element={<AdminDashboard />} />
        <Route path="users" element={<UserManagement />} />
        <Route path="products" element={<ProductManagement />} />
        <Route path="investments" element={<InvestmentManagement />} />
        <Route path="analytics" element={<DataAnalysis />} />
        <Route path="system" element={<SystemManagement />} />
        <Route path="monitoring" element={<MonitoringCenter />} />
        <Route path="security" element={<SecurityCenter />} />
      </Route>
      
      {/* 默认路由 */}
      <Route path="/" element={<Navigate to="/dashboard" replace />} />
      
      {/* 404页面 */}
      <Route path="*" element={<div>页面未找到</div>} />
    </Routes>
  );
};

export default AppRoutes;